package org.juggers.client;

import org.juggers.domain.InvalidCaptchaException;
import org.juggers.domain.JugEvent;
import org.juggers.domain.Jugger;
import org.juggers.remote.JuggersService;
import org.juggers.remote.JuggersServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SubmitButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;

/**
 * Widget to display a registration form
 */
public class RegisterWidget
    extends Composite
{
    interface MyUiBinder
        extends UiBinder<HTMLPanel, RegisterWidget>
    {
    }

    @UiField
    PopupPanel popup;

    @UiField
    FormPanel form;

    @UiField
    TextBox firstName;

    @UiField
    Image firstNameOK;

    @UiField
    TextBox lastName;

    @UiField
    Image lastNameOK;

    @UiField
    TextBox eMail;

    @UiField
    Image eMailOK;
    
    @UiField
    ReCaptcha captcha;

    @UiField
    Image captchaOK;

    @UiField
    SubmitButton ok;
    
//    @UiField
//    TextBox recaptcha_response_field;

    private static final MyUiBinder binder = GWT.create( MyUiBinder.class );

    private static final JuggersServiceAsync service = GWT.create( JuggersService.class );

    private JugEvent event;
    
    public RegisterWidget( JugEvent event )
    {        
        this.event = event;
        initWidget( binder.createAndBindUi( this ) );
        firstNameOK.setVisible( false );
        lastNameOK.setVisible( false );
        eMailOK.setVisible( false );
        captchaOK.setVisible( false );
    }

    public void show()
    {
        popup.setPopupPositionAndShow( new PositionCallback()
        {
            @Override
            public void setPosition( int offsetWidth, int offsetHeight )
            {
                int left = ( Window.getClientWidth() - 300 ) / 2;
                int top = ( Window.getClientHeight() - 100 ) / 2;
                popup.setPopupPosition( left, top );
            }
        } );
    }

    @UiHandler( "ok" )
    public void handleClick( ClickEvent e )
    {
        Jugger jugger = new Jugger( firstName.getText(), lastName.getText(), eMail.getText() );
        // TODO JSR 330 gwt-bean-validation

        service.subscribe( event.getId(), captcha.getChallenge(), captcha.getResponse(), jugger,
            new AsyncCallback<Void>()
        {
            @Override
            public void onSuccess( Void result )
            {
                    Window.alert( "Votre inscription a été enregistrée" );
                popup.hide();
            }
            
            @Override
            public void onFailure( Throwable caught )
            {
            	if ( caught instanceof InvalidCaptchaException )
            	{
            		Window.alert( "Captcha invalide, veuillez le resaisir" );
            		captcha.reload();
            	}
            	else {
                        Window.alert( "Echec de communication avec le serveur, désolé..." );
            	}
            }
        } );
    }

}
